package com.mdb.open.epay.service.epay;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.epay.common.MdbDataSource;
import com.mdb.open.epay.domain.bo.QueryOrderBo;
import com.mdb.open.epay.domain.ko.CustDayOrderStatKo;
import com.mdb.open.epay.domain.ko.StatCustDayOrderKo;
import com.mdb.open.epay.domain.ko.StatUserDayOrderKo;
import com.mdb.open.epay.domain.ko.UserDayOrderStatKo;
import com.mdb.open.epay.domain.po.epay.QpOrder;
import com.mdb.open.epay.enums.status.QpOrderStatusEnum;
import com.mdb.open.epay.mapper.epay.QpOrderMapper;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 类描述：
 * 快捷支付订单表 服务类
 *
 * @author liuyi
 * @date 2019-11-14
 */
@Service
@DS(MdbDataSource.EPAY)
public class QpOrderService extends SuperService<QpOrderMapper, QpOrder> {

    public List<QpOrder> queryPay(QueryOrderBo queryBo) {
        LambdaQueryWrapper<QpOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(ObjectUtil.isNotEmpty(queryBo.getBeginDate()), QpOrder::getCreateTime, queryBo.getBeginDate());
        wrapper.le(ObjectUtil.isNotEmpty(queryBo.getEndDate()), QpOrder::getCreateTime, queryBo.getEndDate());
        if (ObjectUtil.isNotEmpty(queryBo.getOrderNoList())) {
            String[] orderNo = queryBo.getOrderNoList().split(",");
            Set<String> orderNos = new HashSet<>();
            for(String o : orderNo) {
                if(ObjectUtil.isNotEmpty(o)){
                    orderNos.add(o.trim());
                }
            }
            wrapper.in(QpOrder::getCustOrderNo, orderNos);
        }

        wrapper.eq(QpOrder::getCustomerCode, queryBo.getCustomerCode());
        wrapper.eq(QpOrder::getTradeType, queryBo.getBizType());
        wrapper.in(QpOrder::getStatus, QpOrderStatusEnum.SUCCESS.getValue(), QpOrderStatusEnum.FAIL.getValue());

        return this.baseMapper.selectList(wrapper);
    }

    public QpOrder getByCustOrderNo(String customerCode, String orderNo) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<QpOrder>()
                .eq(QpOrder::getCustOrderNo, orderNo)
                .eq(QpOrder::getCustomerCode, customerCode));
    }

    public QpOrder getByInnerOrderNo(String innerOrderNo) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<QpOrder>().eq(QpOrder::getInnerOrderNo, innerOrderNo));
    }

    public List<QpOrder> getByStatus(String qpOrderStatus) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<QpOrder>().eq(QpOrder::getStatus, qpOrderStatus));
    }

    public int updateOrderStatus(String innerOrderNo, String oldStatus, String newStatus) {
        return this.baseMapper.updateOrderStatus(innerOrderNo, oldStatus, newStatus);
    }

    public UserDayOrderStatKo countUserDayOrderInfo(StatUserDayOrderKo ko){
        return this.baseMapper.countUserDayOrderInfo(ko);
    }

    public CustDayOrderStatKo countCustDayOrderInfo(StatCustDayOrderKo ko) {
        return this.baseMapper.countCustDayOrderInfo(ko);
    }

    public boolean existed(String customerCode, String orderNo) {
        return 0 < this.count(new LambdaUpdateWrapper<QpOrder>().eq(QpOrder::getCustOrderNo, orderNo).eq(QpOrder::getCustomerCode, customerCode));
    }
}
